package org.equivalence.server;

import java.util.ArrayList;

import org.equivalence.common.FileRepository;
import org.equivalence.common.FileVersion;


/**
 * return the file differences between two file repository objects
 * @author gcc
 *
 */
class FileRepositoryDiff {

	private ArrayList<String> replace;
	private ArrayList<String> remove;
	private ArrayList<String> add;
	
	public FileRepositoryDiff(FileRepository newest,FileRepository oldest) {
		replace = new ArrayList<String>();
		remove = new ArrayList<String>();
		add = new ArrayList<String>();
		
		//add new or newer files
		for (int i=0;i<newest.getFileCount();++i) {
			FileVersion tempFile = oldest.getFile(newest.getFile(i).getFilePath());
			
			//a new file not in the old repository
			if (tempFile ==null) {
				add.add(newest.getFile(i).getFilePath());
			}
			//or the old repository has an older copy of the file
			else if (tempFile.getVersion()<newest.getFile(i).getVersion()) {
				replace.add(newest.getFile(i).getFilePath());
			}
		}
		
		//remove redundant files
		for (int i=0;i<oldest.getFileCount();++i) {
			FileVersion tempFile = newest.getFile(oldest.getFile(i).getFilePath());
			
			//a file in the old repos not in the new, so remove it
			if (tempFile ==null) {
				remove.add(oldest.getFile(i).getFilePath());
			}		
		}
	}

	/**
	 * @return the add
	 */
	public final ArrayList<String> getAdd() {
		return add;
	}

	/**
	 * @return the remove
	 */
	public final ArrayList<String> getRemove() {
		return remove;
	}

	/**
	 * @return the replace
	 */
	public final ArrayList<String> getReplace() {
		return replace;
	}
}
